﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;



[Serializable]
public class NewsData
{

    public const string IDField = "@ID";

    public const string PRIMARYKEY_Field = @"ID";

    public const string TITLEField = "@Title";

    public const string TXTField = "@Txt";

    public const string CATEGORYField = "@Category";

    public const string TOPField = "@Top";

    public const string MEMBERNAMEField = "@MemberName";

    public const string EDITDATEField = "@EditDate";

    public const string T1Field = "@t1";

    public const string T2Field = "@t2";

    public const string T3Field = "@t3";

    private Guid _ID;

    private String _Title;

    private String _Txt;

    private String _Category;

    private Boolean _Top;

    private String _MemberName;

    private DateTime _EditDate;

    private String _t1;

    private String _t2;

    private String _t3;

    public Guid ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    public String Title
    {
        get { return _Title; }
        set { _Title = value; }
    }
    public String Txt
    {
        get { return _Txt; }
        set { _Txt = value; }
    }
    public String Category
    {
        get { return _Category; }
        set { _Category = value; }
    }
    public Boolean Top
    {
        get { return _Top; }
        set { _Top = value; }
    }
    public String MemberName
    {
        get { return _MemberName; }
        set { _MemberName = value; }
    }
    public DateTime EditDate
    {
        get { return _EditDate; }
        set { _EditDate = value; }
    }
    public String t1
    {
        get { return _t1; }
        set { _t1 = value; }
    }
    public String t2
    {
        get { return _t2; }
        set { _t2 = value; }
    }
    public String t3
    {
        get { return _t3; }
        set { _t3 = value; }
    }
    public NewsData()
    {
        _ID = Guid.Empty;
        _Title = "";
        _Txt = "";
        _Category = "";
        _Top = false;
        _MemberName = "";
        _EditDate = DateTime.Now;
        _t1 = "";
        _t2 = "";
        _t3 = "";
    }
}

public class NewsDataFactory
{
    public static NewsData Construct(IDataReader reader)
    {
        NewsData data = new NewsData();

        try { data.ID = (Guid)(reader["ID"]); }
        catch { }

        try { data.Title = Convert.ToString(reader["Title"]); }
        catch { }

        try { data.Txt = Convert.ToString(reader["Txt"]); }
        catch { }

        try { data.Category = Convert.ToString(reader["Category"]); }
        catch { }

        try { data.Top = Convert.ToBoolean(reader["Top"]); }
        catch { }

        try { data.MemberName = Convert.ToString(reader["MemberName"]); }
        catch { }

        try { data.EditDate = Convert.ToDateTime(reader["EditDate"]); }
        catch { }

        try { data.t1 = Convert.ToString(reader["t1"]); }
        catch { }

        try { data.t2 = Convert.ToString(reader["t2"]); }
        catch { }

        try { data.t3 = Convert.ToString(reader["t3"]); }
        catch { }

        return data;
    }
    public static Guid GetPrimaryKey(DataTable dt)
    {
        return GetPrimaryKey(dt, 0);
    }
    public static Guid GetPrimaryKey(DataTable dt, int rowIndex)
    {
        Guid PrimaryKey = Guid.Empty;

        if (dt.Rows.Count < rowIndex + 1)

            return Guid.Empty;

        DataRow dr = dt.Rows[rowIndex];

        if (dr == null)

            return Guid.Empty;

        try { PrimaryKey = (Guid)(dr[NewsData.PRIMARYKEY_Field]); }
        catch { }

        return PrimaryKey;
    }
    public static NewsData Construct(DataTable dt)
    {
        return Construct(dt, 0);
    }
    public static NewsData Construct(DataTable dt, int rowIndex)
    {
        NewsData data = new NewsData();

        if (dt.Rows.Count < rowIndex + 1)

            return null;

        DataRow dr = dt.Rows[rowIndex];

        if (dr == null)

            return null;

        try { data.ID = new Guid(dr["ID"].ToString()); }
        catch { }

        try { data.Title = Convert.ToString(dr["Title"]); }
        catch { }

        try { data.Txt = Convert.ToString(dr["Txt"]); }
        catch { }

        try { data.Category = Convert.ToString(dr["Category"]); }
        catch { }

        try { data.Top = Convert.ToBoolean(dr["Top"]); }
        catch { }

        try { data.MemberName = Convert.ToString(dr["MemberName"]); }
        catch { }

        try { data.EditDate = Convert.ToDateTime(dr["EditDate"]); }
        catch { }

        try { data.t1 = Convert.ToString(dr["t1"]); }
        catch { }

        try { data.t2 = Convert.ToString(dr["t2"]); }
        catch { }

        try { data.t3 = Convert.ToString(dr["t3"]); }
        catch { }

        return data;
    }
    public static NewsData Construct(DataRow dr)
    {
        NewsData data = new NewsData();

        try { data.ID = new Guid(dr["ID"].ToString()); }
        catch { }

        try { data.Title = Convert.ToString(dr["Title"]); }
        catch { }

        try { data.Txt = Convert.ToString(dr["Txt"]); }
        catch { }

        try { data.Category = Convert.ToString(dr["Category"]); }
        catch { }

        try { data.Top = Convert.ToBoolean(dr["Top"]); }
        catch { }

        try { data.MemberName = Convert.ToString(dr["MemberName"]); }
        catch { }

        try { data.EditDate = Convert.ToDateTime(dr["EditDate"]); }
        catch { }

        try { data.t1 = Convert.ToString(dr["t1"]); }
        catch { }

        try { data.t2 = Convert.ToString(dr["t2"]); }
        catch { }

        try { data.t3 = Convert.ToString(dr["t3"]); }
        catch { }

        return data;
    }
    public static Guid GetPrimaryKey(DataRow dr)
    {
        Guid PrimaryKey = Guid.Empty;

        try { PrimaryKey = (Guid)(dr[NewsData.PRIMARYKEY_Field]); }
        catch { }
        return PrimaryKey;
    }

}
public class NewsDataDBOption
{
    public static int Insert(NewsData d)
    {
        StringBuilder sql = new StringBuilder();
        sql.Append("insert into News (");
        sql.Append("Title, ");
        sql.Append("Txt, ");
        sql.Append("Category, ");
        sql.Append("[Top], ");
        sql.Append("MemberName, ");
        sql.Append("EditDate, ");
        sql.Append("t1, ");
        sql.Append("t2, ");
        sql.Append("t3");
        sql.Append(") values (");
        sql.Append("'");
        sql.Append(d.Title.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("'");
        sql.Append(d.Txt.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("'");
        sql.Append(d.Category.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("");
        sql.Append(Convert.ToInt32(d.Top));
        sql.Append("");
        sql.Append(", ");
        sql.Append("'");
        sql.Append(d.MemberName.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("'");
        sql.Append(d.EditDate.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("'");
        sql.Append(d.t1.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("'");
        sql.Append(d.t2.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("'");
        sql.Append(d.t3.ToString());
        sql.Append("'");
        sql.Append(")");
        return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
    }
    public static int Update(NewsData d)
    {
        StringBuilder sql = new StringBuilder();
        sql.Append("update News set ");
        sql.Append("Title=");
        sql.Append("'");
        sql.Append(d.Title.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("Txt=");
        sql.Append("'");
        sql.Append(d.Txt.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("Category=");
        sql.Append("'");
        sql.Append(d.Category.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("[Top]=");
        sql.Append("");
        sql.Append(Convert.ToInt32(d.Top));
        sql.Append("");
        sql.Append(", ");
        sql.Append("MemberName=");
        sql.Append("'");
        sql.Append(d.MemberName.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("EditDate=");
        sql.Append("'");
        sql.Append(d.EditDate.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("t1=");
        sql.Append("'");
        sql.Append(d.t1.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("t2=");
        sql.Append("'");
        sql.Append(d.t2.ToString());
        sql.Append("'");
        sql.Append(", ");
        sql.Append("t3=");
        sql.Append("'");
        sql.Append(d.t3.ToString());
        sql.Append("'");
        sql.Append(" where ID=");
        sql.Append("'");
        sql.Append(d.ID.ToString());
        sql.Append("'");
        return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
    }
    public static int Delete(NewsData d)
    {
        StringBuilder sql = new StringBuilder();
        sql.Append("delete from News ");
        sql.Append(" where ID='");
        sql.Append(d.ID.ToString()+"'");
        return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
    }
    public static DataTable DataTableSelect()
    {
        StringBuilder sql = new StringBuilder();
        sql.Append("select * from News ");
        return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
    }
    public static DataTable DataTableSelect(NewsData d)
    {
        StringBuilder sql = new StringBuilder();
        sql.Append("select * from News where ID='");
        sql.Append(d.ID.ToString()+"'");
        return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
    }
    public static DataSet DataSetSelect()
    {
        StringBuilder sql = new StringBuilder();
        sql.Append("select * from News ");
        return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
    }
    public static SqlDataReader ReaderSelect(NewsData d)
    {
        StringBuilder sql = new StringBuilder();
        sql.Append("select * from News ");
        return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
    }
    public static void SetTop(NewsData d)
    {
        string sql;
        if (d.Top)
            sql = "UPDATE News SET [Top] =0 WHERE (ID = '" + d.ID + "')";
        else
            sql = "UPDATE News SET [Top] =1 WHERE (ID = '" + d.ID + "')";
        WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql);
    }
}
